package com.yz.xyt.controller;

import com.github.pagehelper.PageInfo;
import com.yz.xyt.constant.Constants;
import com.yz.xyt.constant.HttpStatus;
import com.yz.xyt.domain.Notice;
import com.yz.xyt.domain.ResponseResult;
import com.yz.xyt.domain.vo.NoticeVo;
import com.yz.xyt.service.INoticeService;
import com.yz.xyt.utils.DateUtils;
import com.yz.xyt.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 通知公告controller
 * @author JW.WANG
 */
@RestController
@RequestMapping("notice")
public class NoticeController {
    @Autowired
    INoticeService noticeService;

    /**
     * 获取通知公告列表
     * @param noticeVo
     * @return
     */
    @RequestMapping("/getNoticeList")
    public ResponseResult getNoticeList(NoticeVo noticeVo){
        PageInfo noticePage = noticeService.findForPage(noticeVo);
        ResponseResult result = new ResponseResult();
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("查询成功");
        result.setData(noticePage);
        return  result;
    }

    /**
     * 新增通知公告
     * @param notice
     * @return
     */
    @RequestMapping("/addNotice")
    public ResponseResult addNotice(Notice notice){
        ResponseResult result = new ResponseResult();
        notice.setStatus(Constants.NOTICE_NO_PUBLISH);
        notice.setCreateBy(SecurityUtils.getUsername());
        notice.setCreateTime(DateUtils.getNowDate());
        int count = noticeService.insert(notice);
        if(count > 0){
            result.setCode(HttpStatus.SUCCESS);
            result.setMsg("新增通知公告成功");
        }else{
            result.setCode(HttpStatus.NOT_IMPLEMENTED);
            result.setMsg("新增通知公告失败");
        }
        result.setData(null);
        return result;
    }

    /**
     * 更新公告
     * @param notice
     * @return
     */
    @RequestMapping("/updateNotice")
    public ResponseResult updateNotice(Notice notice){
        ResponseResult result = new ResponseResult();
        notice.setUpdateBy(SecurityUtils.getUsername());
        notice.setUpdateTime(DateUtils.getNowDate());
        int count = noticeService.updateByPrimaryKeySelective(notice);
        if(count > 0){
            result.setCode(HttpStatus.SUCCESS);
            result.setMsg("更新通知公告成功");
        }else{
            result.setCode(HttpStatus.NOT_IMPLEMENTED);
            result.setMsg("更新通知公告失败");
        }
        result.setData(null);
        return result;
    }

    /**
     * 获取通知公告详情
     * @param notice
     * @return
     */
    @RequestMapping("/getNoticeDetail")
    public ResponseResult getNoticeDetail(Notice notice){
        ResponseResult result = new ResponseResult();
        notice = noticeService.selectByPrimaryKey(notice.getId());
        if(!Objects.isNull(notice)){
            result.setCode(HttpStatus.SUCCESS);
            result.setMsg("查询通知公告详情成功");
        }else{
            result.setCode(HttpStatus.NOT_IMPLEMENTED);
            result.setMsg("查询通知公告详情失败");
        }
        result.setData(notice);
        return result;
    }

    /**
     * 发布通知公告
     * @param notice
     * @return
     */
    @RequestMapping("/publishNotice")
    public ResponseResult publishNotice(Notice notice){
        ResponseResult result = new ResponseResult();
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("发布通知公告成功");

        notice.setStatus(Constants.NOTICE_PUBLISH);
        notice.setUpdateBy(SecurityUtils.getUsername());
        notice.setUpdateTime(DateUtils.getNowDate());

        int count = noticeService.updateByPrimaryKeySelective(notice);
        if(count == 0){
            result.setCode(HttpStatus.NOT_IMPLEMENTED);
            result.setMsg("发布通知公告失败");
        }
        result.setData(null);
        return result;
    }

    /**
     * 删除通知公告
     * @param notice
     * @return
     */
    @RequestMapping("/deleteNotice")
    public ResponseResult deleteNotice(Notice notice){
        ResponseResult result = new ResponseResult();
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("删除通知公告成功");

        int count = noticeService.deleteByPrimaryKey(notice.getId());
        if(count == 0){
            result.setCode(HttpStatus.NOT_IMPLEMENTED);
            result.setMsg("删除通知公告失败");
        }
        result.setData(null);
        return  result;
    }

    /**
     * 下架通知公告
     * @param notice
     * @return
     */
    @RequestMapping("/offNotice")
    public ResponseResult offNotice(Notice notice){
        ResponseResult result = new ResponseResult();
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("下架通知公告成功");

        notice.setStatus(Constants.NOTICE_OFF);
        notice.setUpdateBy(SecurityUtils.getUsername());
        notice.setUpdateTime(DateUtils.getNowDate());

        int count = noticeService.updateByPrimaryKeySelective(notice);
        if(count == 0){
            result.setCode(HttpStatus.NOT_IMPLEMENTED);
            result.setMsg("下架通知公告失败");
        }
        result.setData(null);
        return result;
    }
}
